<?php

/**
 * @filename Money.php
 * @encoding UTF-8
 *
 * @author 徐小军
 *
 * @link http://www.okd100.com
 *
 * @copyright 南昌欧科达科技有效公司
 * @datetime 2016-7-28  10:47:13
 *
 * @version 1.0
 * @Description 货币管理
 */

namespace app\admin\controller;

use app\common\model\User as UserModel;
use app\common\model\Encashment as EncashmentModel;
use app\common\model\CashFlow as CashFlowModel;

class Money extends Common
{
    /**
      * 申请提现.
      */
     public function applyMoney($cash_flow_status = 0)
     {
         $usr = UserModel::get(session('user_id'));
         if (IS_POST) {
             $card = $usr->vaild_card;
             $data = input('post.');
             if ($data['cash_value'] <= cache('config_business')['business_lowMoney']) {
                 return error('取现金额要大于'.cache('config_business')['business_lowMoney']);
             }
             if ($data['cash_value'] % cache('config_business')['business_lowMoneyMultiple'] != 0) {
                 return error('取现金额要是'.cache('config_business')['business_lowMoneyMultiple'].'的整数倍');
             }
             if (!$usr->vaild_card) {
                 return error('没有有效银行卡');
             }
             if ($usr->money_cash < $data['cash_value']) {
                 return error('余额不足');
             }
             $res = $usr->encashments()->save([
               'cash_value' => $data['cash_value'],
               'card_id' => $card->card_id,
             ]);
             if (false === $result) {
                 return error($User->getError());
             }

             return success('申请成功');
         } else {
             if (input('startdate') && input('enddate')) {
                 $startdate = input('startdate');
                 $enddate = input('enddate');
                 $where['create_time'] = ['>=', $startdate];
                 $where['create_time'] = ['<=', $enddate];
             }
             $list = $usr->encashments()->where($where);
             if ($cash_flow_status != 0) {
                 $list = $list->where('cash_flow_status', $cash_flow_status);
             }
             $list = $list->paginate(getPageSize());
             table_empty_list($list);
             $this->assign('startdate', $startdate);
             $this->assign('enddate', $enddate);
             $this->assign('cash_flow_status', $cash_flow_status);
             $this->assign('user', $usr);
             $this->assign('encashmentlist', $list);

             return $this->fetch();
         }
     }
    public function mod_encashment($encashment_id)
    {
      if(session('user')->user_type=='user' or session('user')->user_type=='agent')
      {
        return error('没有权限');
      }
        if (IS_POST) {
            $enc = EncashmentModel::get($encashment_id);
            $data = input('post.');
            if (!$enc) {
                return error('不存在这个提现');
            }
            if ($enc->cash_flow_type != '提现') {
                return error('这不是一个提现');
            }
            if ($data['action'] == '确认打款') {
                $enc->confirm_user = session('user_id');
                $enc->cash_flow_status = '已完成';
                $enc->encashed_value = $enc->cash_value;
                $enc->cash_flow_comments .= $data['comment'];
                $para = config('msg.send_encashment_confirmed');
                $para['content'] = strtr($para['content'], ['%comment%' => $enc->cash_flow_comments]);
                UserModel::get($enc->target_user->user_id)->mails()->save($para);
            } elseif ($data['action'] == '打款失败') {
                $enc->confirm_user = session('user_id');
                $enc->cash_flow_status = '失败';
                $enc->cash_flow_comments .= $data['comment'];
                $para = config('msg.send_encashment_fail');
                $para['content'] = strtr($para['content'], ['%comment%' => $enc->cash_flow_comments]);
                UserModel::get($enc->target_user->user_id)->mails()->save($para);
            } elseif ($data['action'] == '修改备注') {
                $enc->cash_flow_comments = $data['comment'];
            } else {
                return error('参数错误');
            }
            $enc->save();

            return success('操作成功');
        } else {
            return '非法访问';
        }
    }
     /**
      * 管理员处理提现列表.
      */
     public function applyMoneyManger($cash_flow_status = 0)
     {
       if(session('user')->user_type=='user' or session('user')->user_type=='agent')
       {
         return error('没有权限');
       }
         if (input('startdate') && input('enddate')) {
             $startdate = input('startdate');
             $enddate = input('enddate');
             $where['create_time'] = ['>=', $startdate];
             $where['create_time'] = ['<=', $enddate];
         }
         if ($keyword = input('keyword')) {
             $user = UserModel::where('user_name|user_uniid', 'like', '%'.$keyword.'%')->field('user_id')->select();
             foreach ($user as $u) {
                 $user_ids[] = $u['user_id'];
             }
             if ($user_ids) {
                 $where['user_id'] = ['in', $user_ids];
             }
         }
         $list = EncashmentModel::where($where)->where('cash_flow_type', 3);
         if ($cash_flow_status != 0) {
             $list = $list->where('cash_flow_status', $cash_flow_status);
         }
         $list = $list->paginate(getPageSize());
         table_empty_list($list);
         $this->assign('startdate', $startdate);
         $this->assign('enddate', $enddate);
         $this->assign('keyword', $keyword);
         $this->assign('cash_flow_status', $cash_flow_status);
         $this->assign('user', $usr);
         $this->assign('encashmentlist', $list);

         return $this->fetch();
     }

      /**
       *  所有货币流向查询.
       */
      public function moneyList($cash_flow_type = -1)
      {
        if(session('user')->user_type=='user' or session('user')->user_type=='agent')
        {
          return error('没有权限');
        }
          if (input('startdate') && input('enddate')) {
              $startdate = input('startdate');
              $enddate = input('enddate');
              $where['create_time'] = ['>=', $startdate];
              $where['create_time'] = ['<=', $enddate];
          }
          if ($keyword = input('keyword')) {
              $user = UserModel::where('user_name|user_uniid', 'like', '%'.$keyword.'%')->field('user_id')->select();
              foreach ($user as $u) {
                  $user_ids[] = $u['user_id'];
              }
              $where['user_id'] = ['in', $user_ids];
          }
          $list = CashFlowModel::where($where);


         // echo  CashFlowModel::fetchSql()->where($where)->where('cash_flow_status', '2');exit;
          if ($cash_flow_type == -1) {
            //  $list = $list->where('cash_flow_type', '<>', 2);
          } else {
              if ($cash_flow_type == 2 or $cash_flow_type == 0) {
                 /// $list->order("b.cash_flow_id desc");
                  $list = $list->where('cash_flow_status', 'in', array(0,1,2,3,4));
                  $award_type = ceil($cash_flow_type / 2);
                  $list = $list->where('cash_flow_type', 0);
                  $list = $list->alias('a')->join('ycyp_award b', 'a.cash_flow_id = b.cash_flow_id');
                  $list = $list->where('b.award_type', $award_type);
              }elseif($cash_flow_type == 4){

                  $award_type = 2;
                  $list = $list->where('cash_flow_type', 0);
                  $list = $list->where('cash_flow_status','in', array(0,1,2,3,4));
                  $list = $list->alias('a')->join('ycyp_award b', 'a.cash_flow_id = b.cash_flow_id');
                  $list = $list->where('b.award_type', $award_type);
                 // $list->order("b.cash_flow_id desc");
              }
              else {

                  $list = $list->where('cash_flow_status', '2');
                  $list = $list->where('cash_flow_type', $cash_flow_type);
              }

              $count = clone $list;
              $count = $count->sum('cash_value');
              $this->assign('count', $count);
          }
          $list = $list->paginate(getPageSize());
          table_empty_list($list);

          $this->assign('startdate', $startdate);
          $this->assign('enddate', $enddate);
          $this->assign('keyword', $keyword);
          $this->assign('cash_flow_type', $cash_flow_type);
          $this->assign('list', $list);

          return $this->fetch();
      }
}
